package com.corpgovernment.common.logging.service;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

import com.corpgovernment.common.logging.service.impl.LoggerServiceImpl;

/**
 *  @author: pwang27
 *  @Date: 2023/11/8 15:21
 *  @Description: 日志工厂类
 */
public class LoggerServiceFactory {

    private static final Map<Hash, LoggerService> MAP = new ConcurrentHashMap<>();

    private LoggerServiceFactory() {}

    /**
     * 获得默认的LogService
     */
    public static LoggerService getLoggerService(Class<?> clazz) {
        return MAP.computeIfAbsent(new Hash(clazz), h -> new LoggerServiceImpl(h.clazz));
    }

    static class Hash {
        private final Class<?> clazz;

        public Hash(Class<?> clazz) {
            this.clazz = clazz;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            Hash hash = (Hash)o;
            return Objects.equals(clazz, hash.clazz);
        }

        @Override
        public int hashCode() {
            return Objects.hash(clazz);
        }
    }
}
